﻿Imports System.Net
Imports System.IO
Imports System.Security.AccessControl
Imports System
Public Class UpdateManager


    Private Sub Start_UpdateCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start_UpdateCheck.Click
        Start_UpdateCheck.Enabled = False
        UpdateCheck_progressbar.Value = 0
        Status.Text = ""
        'check server
        UpdateCheck_progressbar.Maximum = 10
        UpdateCheck_progressbar.Step = 10
        Status.Text = "Connecting..."
        UpdateCheck_progressbar.Value = 1
        Try
            My.Computer.Network.Ping("dev-network.com")
        Catch ex As Exception
            MsgBox("Unable to coneect to server. It may be having difficulties or your internet connection may be having problems.")
            Status.Text = "Error - unable to connect. Please try again later!"
        End Try
        Status.Text = "Connected to Version Server."
        UpdateCheck_progressbar.PerformStep()
        Status.Text = "Downloading Version File..."
        UpdateCheck_progressbar.PerformStep()
        'download file
        DownloadClass.GetFile(My.Resources.Update_URL, System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Version\")
        Status.Text = "Version File Download - Complete!"
        UpdateCheck_progressbar.Value = 5
        ' Check version
        Status.Text = "Opening Version file..."
        UpdateCheck_progressbar.PerformStep()
        Dim line As String
        Status.Text = "Checking Downloaded Version Code..."
        UpdateCheck_progressbar.PerformStep()
        Using reader As StreamReader = New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Version\version.txt")
            line = reader.ReadLine
        End Using
        Status.Text = "Validating Newest Build Number..."
        UpdateCheck_progressbar.PerformStep()
        Dim versionVar As String() = line.Split(",")
        Status.Text = "Validating Newest Build Number..."
        UpdateCheck_progressbar.PerformStep()

        'complete
        UpdateCheck_progressbar.ForeColor = Color.Red
        UpdateCheck_progressbar.PerformStep()
        Status.Text = "Complete."
        UpdateCheck_progressbar.PerformStep()
        If CInt(versionVar(0)) > My.Resources.Build_Version Then
            Download_Update.Visible = True
            Status.Text = "New Version Available!"
            Me.Text = "Update Manager - New version available!"
        Else
            MsgBox("You are running the most recent version of WSI!")
            Me.Close()
            Me.Dispose()
        End If


    End Sub

    Private Sub Download_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Download_Update.Click
        UpdateCheck_progressbar.Value = 0
        Status.Text = "Downloading update..."
        Dim line As String
        Using reader As StreamReader = New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Version\version.txt")
            line = reader.ReadLine
        End Using
        Dim versionVar As String() = line.Split(New Char() {","c})
        Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\")
        Try
            DownloadClass.GetFile(versionVar(1), System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\")
        Catch ex As Exception
            Status.Text = "Error downloading update. Please try again later."
            UpdateCheck_progressbar.Value = 0
        End Try
        UpdateCheck_progressbar.Value = 5
        Status.Text = "Download complete. Launching installer..."
        'SHELL EXECUTE FILE!
        Dim filelocation As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\" & versionVar(2)
        'filelocation = filelocation.Replace("\", "/")
        Try
            Process.Start(filelocation)
        Catch ex As Exception
            MsgBox("Error executing file. Please try re-opening the update manager or executing the file manually from the Update folder of this installation.", MsgBoxStyle.Critical)
            MsgBox(ex.ToString)
            MsgBox(versionVar(2))
            MsgBox(filelocation)
        End Try
        UpdateCheck_progressbar.Value = 10
        Me.Close()
        Me.Dispose()
        MainWindow.Close()
        MainWindow.Dispose()
    End Sub


    Private Sub UpdateManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = My.Resources.icon
    End Sub
End Class